package com.tis.std.batch.common;

import java.util.List;

import org.apache.log4j.Logger;

import promis.utility.CommonService;
import promis.utility.QueueProcessAction;
import promis.utility.Utilitys;

import apps.sessionFactory;

import com.sun.corba.se.impl.util.Utility;
import com.tis.std.commons;

public class PropertyUnitBatch extends commons {
	
	private static final Logger log = Logger.getLogger(PropertyUnitBatch.class);

	private void BatchExec(String company, String project_opt, String buildingPhase_opt, String FloowZone_opt) throws Exception {
		try{			
			StringBuilder _strQuery = new StringBuilder();
			
			_strQuery.append(" SELECT ");
			_strQuery.append(" COMPANY, PROJECT, BUILDING_PHASE, ZONE_CODE, UNIT_NUMBER, CREATED_BY ");  
			_strQuery.append(" FROM  SPL.VW_SYNC_COST_PROPUNIT ");
			_strQuery.append(" WHERE COMPANY = ").append(Utilitys.strSql(company));
			
			if(!CommonService.IsNull(project_opt))
				_strQuery.append(" AND PROJECT = ").append(Utilitys.strSql(project_opt));
			
			if(!CommonService.IsNull(buildingPhase_opt))
				_strQuery.append(" AND BUILDING_PHASE = ").append(Utilitys.strSql(buildingPhase_opt));
			
			if(!CommonService.IsNull(FloowZone_opt))
				_strQuery.append(" AND ZONE_CODE = ").append(Utilitys.strSql(FloowZone_opt));
			
			List<Object[]> result = (List<Object[]>) sessionFactory.CreateEmPromis().createNativeQuery(_strQuery.toString()).getResultList();
			System.out.println("result size =" + CommonService.getSize(result));
			
			if(!CommonService.IsNull(result)){
				try{
					for (Object[] value : result) {	
						CleanValue();
						
						setAction(promis.utility.InterfaceAction.PropertyUnit.PER_UNIT.toString());
						setCompany(CommonService.IsNullRNull(value[0]));
						setProject(CommonService.IsNullRNull(value[1]));
						setBuildingPhase(CommonService.IsNullRNull(value[2]));
						setZoneCode(CommonService.IsNullRNull(value[3]));
						setUnitNumber(CommonService.IsNullRNull(value[4]));
						setProcessAction(QueueProcessAction.BATCH);
						setProcessActionBy("USER_BATCH");
						
						new apps.promis.out.common.SyncQueueAction().PutQueue(	promis.utility.BusinessModule.COMMONS.toString(), 
																				promis.utility.ProcessModule.PROPERTY_UNIT.toString(),
																				null,
																				QueueProcessAction.BATCH,
																				ConvertParamToHashMap()
																				);
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}catch(Exception e){
			log.error(e.getMessage());
			e.printStackTrace();
			throw e;
		}
	}

	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try{
			
			new PropertyUnitBatch().BatchExec("SP", "SVSP", "02", null);
			
		}catch(Exception e){
			log.error(e.getMessage());
		}
	}

}
